<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="模块名称" prop="moduleName">
        <el-input
          v-model="queryParams.moduleName"
          placeholder="请输入模块名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模块标题" prop="moduleTitle">
        <el-input
          v-model="queryParams.moduleTitle"
          placeholder="请输入模块标题"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable size="small" style="width: 240px">
          <el-option
            v-for="item in statusOptions"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" max-height="600" style="width: 100%">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="moduleName"  width="100" />
      <el-table-column label="标题" align="center" prop="moduleTitle"  width="100" />
      <el-table-column label="专题图" align="center" width="200" prop="modulePic" >
        <template slot-scope="scope">
          <img :src="scope.row.modulePic" width="200"/>
        </template>
      </el-table-column>
      <el-table-column label="商品列数" align="center" prop="itemCol" />
      <el-table-column label="跳转类型" align="center" prop="linkType" :formatter="linkTypeFormat"/>
      <el-table-column label="跳转链接" align="center" prop="linkUrl" width="300" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'primary' : 'danger'">{{statusFormat(scope.row.status)}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" width="60"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="修改人" align="center" prop="updateBy" width="60"/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180" />
      <el-table-column fixed="right" label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改菜单对话框 -->
    <el-dialog center :title="title" :visible.sync="open" width="60%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="专题名称">
              <el-input v-model="form.moduleName" maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="专题标题">
              <el-input v-model="form.moduleTitle" maxlength="100" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="跳转类型" prop="linkType">
              <el-select v-model="form.linkType" placeholder="请选择">
                <el-option
                  v-for="item in this.linkTypeOptions"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="跳转链接" prop="linkUrl">
              <el-input v-model="form.linkUrl" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="专题图" prop="modulePic">
              <el-upload
                :action="uploadFile"
                :headers="headers"
                class="image-uploader"
                :show-file-list="false"
                :before-upload="beforeImageUpload"
                :on-success="handleImageUploadSuccess"
                accept=".jpg,.jpeg,.png">
                <img v-if="form.modulePic" :src="form.modulePic" class="banner-image">
                <i v-else class="el-icon-plus image-uploader-icon"></i>
              </el-upload>
              <span>建议尺寸：640*320，比例2:1，最大500kb</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="显示排序" prop="sort">
              <el-input-number v-model="form.sort" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">正常</el-radio>
                <el-radio :label="0">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="商品列数">
              <el-input-number v-model="form.itemCol" controls-position="right" :min="0" :max="3" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { listSubject, getSubject } from "@/api/content/subject";
  import { delModule } from "@/api/content/content";
  import { getToken } from '@/utils/auth'

  export default {
    name: "SubjectList",
    data() {
      return {
        uploadFile: process.env.VUE_APP_BASE_API + "/file/upload?path=subject",
        headers: {
          Authorization: 'Bearer ' + getToken()
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        linkTypeOptions: [],
        statusOptions: [],
        // 总条数
        total: 0,
        // 表格数据
        list: [],
        // 查询参数
        queryParams: {
          page: 1,
          size: 20,
          moduleName: undefined,
          moduleTitle: undefined,
          status: undefined
        },
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          linkType: [
            { required: true, message: "请选择跳转类型", trigger: "blur" }
          ],
          modulePic: [
            { required: true, message: "请上传轮播图", trigger: "blur" }
          ],
          sort: [
            { required: true, message: "菜单顺序不能为空", trigger: "blur" }
          ],
          status: [
            { required: true, message: "请选择状态", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getOptions("status").then(response => {
        this.statusOptions = response.data.options;
      });
      this.getOptions("link-type").then(response => {
        this.linkTypeOptions = response.data.options;
      });
    },
    methods: {
      /** 查询商品类型列表 */
      getList() {
        this.loading = true;
        listSubject(this.queryParams).then(response => {
            this.list = response.data.rows;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.createTime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.multiple = !selection.length
      },
      statusFormat(data) {
        return this.selectOptionValue(this.statusOptions, data);
      },
      linkTypeFormat(row, column) {
        return this.selectOptionValue(this.linkTypeOptions, row.linkType);
      },
      beforeImageUpload(file) {
        const isLt = file.size / 1024 < 500;
        if (!isLt) {
          this.$message.error('上传头像图片大小不能超过 500KB!');
        }
        return isLt;
      },
      handleImageUploadSuccess(res, file) {
        this.form.modulePic = res.data.url;
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          moduleName: undefined,
          moduleTitle: undefined,
          linkType: undefined,
          linkUrl: undefined,
          modulePic: undefined,
          sort: 0,
          itemCol: 0,
          status: 1,
          itemList: []
        };
        this.resetForm("form");
      },
      cancel() {
        this.open = false;
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        getSubject(row.id).then(res => {
          this.form = res.data;
          this.open = true;
          this.title = "修改轮播图";
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id ? [row.id] : this.ids;
        this.$confirm('是否删除：' + ids + '?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delModule(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加专题";
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
          }
        });
      }
    }
  };
</script>

<style>
  .image-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .image-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .image-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 600px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .banner-image {
    width: 600px;
    height: 100px;
    display: block;
  }
</style>
